package net.cloudsun.graph.puzzle;

import com.youngthing.displace.DisplaceExpression;
import com.youngthing.displace.DisplaceExpressions;

import java.io.File;
import java.util.List;
import java.util.Random;

/**
 * 拼图游戏
 *
 * @author Ryan
 * @since
 */
public class Main {

    public static void main(String[] args) {
        // 先造一个状态作为初态
        long start = System.currentTimeMillis();
        JigsawGraph jigsawGraph = new JigsawGraph(new File("jigsaw-vertices.dat"));
        long end = System.currentTimeMillis();
        System.out.println(end - start + " 毫秒");
        int length = jigsawGraph.getVertices().length;
        int index = new Random().nextInt(length);
        JigsawState tofind = jigsawGraph.getVertices()[index];
        System.out.println("试图解开\n" + tofind.toNumberString());
        System.out.println("初始化完毕");
        final JigsawState finalState = jigsawGraph.getVertices()[JigsawGraph.FINAL_STATE_INDEX];
        final int[] finalArray = finalState.toPlainArray();
        List<JigsawState> path = jigsawGraph.bfs(JigsawGraph.FINAL_STATE_INDEX, index);
        path.forEach(x ->
        {
            final DisplaceExpression expression = DisplaceExpressions.compare(finalArray, x.toPlainArray());
            System.out.println(expression);
            System.out.println(x.toNumberString());
        });
        if (path.isEmpty()) {
            System.out.println("无解");
        } else {
            System.out.println("解开总共需要" + path.size() + "步");
        }
    }


}
